package com.lee.utils;

import com.alibaba.fastjson.JSON;
import com.lee.logger.Logger;
import com.lee.source.HbaseSource;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

public class SupportDataUtils {
    private static String tableName = "marketing:supportdata";
    private static String family = "info";
    private static String supportQualifier = "support";
    private static String numQualifier = "num";
    public static void initMap() throws IOException {
        HbaseSource.truncate(tableName);
    }
    public static boolean judgeKeyInMap(List<String> key) throws IOException {
        key.sort(String::compareTo);
        return (judgeKeyInMap(key.toString()));

    }
    public static boolean judgeKeyInMap(String key) throws IOException {

        String result = HbaseSource.get(key,tableName,family,supportQualifier);
        if(! "".equals(result)){
            return true;
        }else {
            return false;
        }

    }

    public static boolean judgeKeyInMapNum(String key) throws IOException {

        String result = HbaseSource.get(key,tableName,family,numQualifier);
        if(! "".equals(result)){
            return true;
        }else {
            return false;
        }

    }
    public static void setValueByKey(List<String> key,double value) throws IOException {
        key.sort(String::compareTo);
        String dataStr = String.valueOf(value);
        HbaseSource.put(key.toString(),tableName,family,supportQualifier,dataStr);
    }

    public static void setValueByKeyNum(List<String> key,long value) throws IOException {
        key.sort(String::compareTo);
        String dataStr = String.valueOf(value);
        HbaseSource.put(key.toString(),tableName,family,numQualifier,dataStr);
    }

    public static void addSupportNum(List<String> key) throws IOException {
        long value = getValueByKeyNum(key);
        value += 1;
        String dataStr = String.valueOf(value);
        HbaseSource.put(key.toString(),tableName,family,numQualifier,dataStr);
    }

    public static double getValueByKey(List<String> key) throws IOException {
        key.sort(String::compareTo);
        if(judgeKeyInMap(key)){
            String data = HbaseSource.get(key.toString(),tableName,family,supportQualifier);
            double dataD = Double.parseDouble(data);
            return dataD;
        } else {
            return 0;
        }

    }


    public static long getValueByKeyNum(List<String> key) throws IOException {
        key.sort(String::compareTo);
        return getValueByKeyNum(key.toString());

    }

    public static long getValueByKeyNum(String key) throws IOException {
        if(judgeKeyInMapNum(key)){
            String data = HbaseSource.get(key,tableName,family,numQualifier);
            long dataD = Long.parseLong(data);
            return dataD;
        } else {
            return 0;
        }

    }

    public static List<String> getAllKey() throws IOException {
        List<String> allKey = HbaseSource.getAllKey(tableName);
        return allKey;
    }


    public static void delSupportNum(String key) throws IOException {
        long value = getValueByKeyNum(key);
        value -= 1;
        if(value <= 0){
            HbaseSource.delete(key,tableName);
        } else {
            String dataStr = String.valueOf(value);
            HbaseSource.put(key,tableName,family,numQualifier,dataStr);
        }

    }



}